//
//  UserInfoViewController.m
//  UHealth
//
//  Created by liliang on 15/2/12.
//  Copyright (c) 2015年 gjtx. All rights reserved.
//

#import "UserInfoViewController.h"



@interface UserInfoViewController ()<UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UIImageView                    *headIconImageView;
@property (weak, nonatomic) IBOutlet UITextField                    *nameField;
@property (weak, nonatomic) IBOutlet UIButton                       *maleBtn;
@property (weak, nonatomic) IBOutlet UIButton                       *femaleBtn;
@property (weak, nonatomic) IBOutlet UITextField                    *ageField;
@property (weak, nonatomic) IBOutlet UITextField                    *heightField;
@property (weak, nonatomic) IBOutlet UITextField                    *weightField;
@property (weak, nonatomic) IBOutlet UIButton                       *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton                       *registerBtn;
@property (weak, nonatomic) IBOutlet UIButton                       *skipBandBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint             *bottomSpaceConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint             *bandBtnTopSpaceConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint             *loginBtnTopSpaceConstraint;

@property (nonatomic, strong)UIControl                              *bgCtl;

@end

@implementation UserInfoViewController

#define kWidth                  ([UIScreen mainScreen].bounds.size.width)
#define kHeight                 ([UIScreen mainScreen].bounds.size.height)


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    if (VIEW_3_5_INCH) {
        self.bottomSpaceConstraint.constant      = 3.0f;
        self.bandBtnTopSpaceConstraint.constant  = 4.0f;
        self.loginBtnTopSpaceConstraint.constant = 3.0f;
    }
    else {
        self.bottomSpaceConstraint.constant      = 8.0f;
        self.bandBtnTopSpaceConstraint.constant  = 35.0f;
        self.loginBtnTopSpaceConstraint.constant = 8.0f;
    }
    [UIView animateWithDuration:0.3 animations:^{
        [self.view updateConstraintsIfNeeded];
        [self.view layoutIfNeeded];
    }];
    self.headIconImageView.layer.cornerRadius  = 32.5f;
    self.headIconImageView.layer.masksToBounds = YES;
    self.skipBandBtn.layer.cornerRadius        = 5.0f;
    self.loginBtn.layer.cornerRadius           = 3.0f;
    self.loginBtn.layer.borderWidth            = 1.0f;
    self.loginBtn.layer.borderColor            = [UIColor colorWithRed:230/255.0f green:230/255.0f blue:230/255.0f alpha:1.0].CGColor;
    self.registerBtn.layer.cornerRadius        = 3.0f;
    self.registerBtn.layer.borderColor         = [UIColor colorWithRed:230/255.0f green:230/255.0f blue:230/255.0f alpha:1.0].CGColor;
    self.registerBtn.layer.borderWidth         = 1.0f;
}

#pragma mark - UIPickerView
//picker type 0为年龄，1为身高，2为体重
- (void)showPickViewWithType:(NSInteger)pickerType
{
    if (self.bgCtl) {
        [self removePickerView];
    }
    
    self.bgCtl = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    [self.bgCtl addTarget:self action:@selector(removePickerView) forControlEvents:UIControlEventTouchUpInside ];
    self.bgCtl.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.bgCtl];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeight, kWidth, 180)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.tag = 1000;
    [self.bgCtl addSubview:bgView];
    
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kWidth, 40)];
    UIBarButtonItem *cancelBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelBarBtnClicked)];
    UIBarButtonItem *spaceBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *confirmBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"Confirm" style:UIBarButtonItemStyleBordered target:self action:@selector(confirmBarBtnClicked)];
    toolbar.items = @[cancelBarBtn, spaceBarBtn, confirmBarBtn];
    [bgView addSubview:toolbar];
    
    if (pickerType == 0) {
        UIDatePicker *datePicker  = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, kWidth, 140)];
        datePicker.maximumDate    = [NSDate date];
        datePicker.minimumDate    = [NSDate dateWithTimeIntervalSince1970:0];
        datePicker.datePickerMode = UIDatePickerModeDate;
        datePicker.date           = [NSDate dateWithTimeIntervalSince1970:0];
        datePicker.tag            = 2000 + pickerType;
        [bgView addSubview:datePicker];
    }
    else {
        UIPickerView *numberPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, kWidth, 140)];
        numberPicker.delegate      = self;
        numberPicker.dataSource    = self;
        numberPicker.tag           = 2000 + pickerType;
        [bgView addSubview:numberPicker];
    }
    
    [UIView animateWithDuration:0.5 animations:^{
        bgView.center = CGPointMake(kWidth/2, kHeight-160/2-64+5);
    }];
}

- (void)cancelBarBtnClicked
{
    [self removePickerView];
}

- (void)confirmBarBtnClicked
{
    [self removePickerView];
}

- (void)removePickerView
{
    [UIView animateWithDuration:0.5 animations:^{
        UIView *bgView = [self.bgCtl viewWithTag:1000];
        bgView.center = CGPointMake(kWidth/2, kHeight + 120/2);
    } completion:^(BOOL finished) {
        for (UIView *subView in self.bgCtl.subviews) {
            [subView removeFromSuperview];
        }
        if (self.bgCtl) {
            [self.bgCtl removeFromSuperview];
            self.bgCtl = nil;
        }
    }];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == self.nameField) {
        return YES;
    }
    else if (textField == self.ageField) {
        [self showPickViewWithType:0];
        return NO;
    }
    else if (textField == self.heightField) {
        [self showPickViewWithType:1];
        return NO;
    }
    else if (textField == self.weightField) {
        [self showPickViewWithType:2];
        return NO;
    }
    else {
        return NO;
    }
}

#pragma mark - UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

//picker type 0为年龄，1为身高，2为体重
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView.tag == 2000) {
        return 0;
    }
    else if (pickerView.tag == 2001) {
        return 70;
    }
    else if (pickerView.tag == 2002) {
        return 120;
    }
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView.tag == 2001) {
        return [NSString stringWithFormat:@"%dcm", row+80];
    }
    else if (pickerView.tag == 2002) {
        return [NSString stringWithFormat:@"%d kg",row+30];
    }
    else {
        return nil;
    }
}

//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
//{
//    return nil;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
